Bootstrap demo

Python Sets: Comprehensive Notes for Students

Table of Contents

1. Introduction to Sets

In Python, a set is an unordered collection of unique, immutable objects. Sets are mutable, meaning you can add and remove items, but the items themselves must be immutable (like numbers, strings, or tuples).

Key Characteristics:

2. Creating Sets

Sets can be created using curly braces {} or the set() constructor:

# Using curly braces
fruits = {"apple", "banana", "cherry"}
print(fruits) # Output: {'apple', 'banana', 'cherry'}

# Using set() constructor
numbers = set([1, 2, 3, 4, 5])
print(numbers) # Output: {1, 2, 3, 4, 5}

# Empty set (must use set(), not {})
empty_set = set()
print(empty_set) # Output: set()
Note: {} creates an empty dictionary, not an empty set. Use set() for empty sets.

3. Set Properties

Unordered Nature:

my_set = {"a", "b", "c", "d"}
print(my_set) # Order may vary: {'c', 'a', 'b', 'd'}

No Duplicates:

duplicates = {1, 2, 2, 3, 3, 3, 4}
print(duplicates) # Output: {1, 2, 3, 4}

4. Set Operations

Operation Syntax Description Example
Union | or union() Elements in either set A | B
Intersection & or intersection() Elements in both sets A & B
Difference - or difference() Elements in A but not B A - B
Symmetric Difference ^ or symmetric_difference() Elements in either set but not both A ^ B

5. Set Methods

Method Description Example
add(element) Adds an element to the set s.add(5)
remove(element) Removes an element (raises error if not found) s.remove(5)
discard(element) Removes element (no error if not found) s.discard(5)
pop() Removes and returns arbitrary element s.pop()
clear() Removes all elements s.clear()
update(iterable) Adds multiple elements s.update([1,2,3])

6. Frozen Sets

Frozen sets are immutable versions of sets. Once created, they cannot be modified.

# Creating a frozen set
frozen = frozenset([1, 2, 3, 4])
print(frozen) # Output: frozenset({1, 2, 3, 4})

# Frozen sets are immutable
# frozen.add(5) # This would raise an AttributeError

7. Practical Applications

Removing Duplicates:

numbers = [1, 2, 2, 3, 4, 4, 5]
unique_numbers = list(set(numbers))
print(unique_numbers) # Output: [1, 2, 3, 4, 5]

Membership Testing:

vowels = {'a', 'e', 'i', 'o', 'u'}
print('a' in vowels) # Output: True
print('x' in vowels) # Output: False

8. Code Examples

Basic Set Operations:

A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}

print("Union:", A | B) # {1, 2, 3, 4, 5, 6, 7, 8}
print("Intersection:", A & B) # {4, 5}
print("Difference:", A - B) # {1, 2, 3}
print("Symmetric Diff:", A ^ B) # {1, 2, 3, 6, 7, 8}

Set Comprehension:

# Create set of squares
squares = {x**2 for x in range(10)}
print(squares) # {0, 1, 4, 9, 16, 25, 36, 49, 64, 81}

# Create set of even numbers
evens = {x for x in range(20) if x % 2 == 0}
print(evens) # {0, 2, 4, 6, 8, 10, 12, 14, 16, 18}
Tip: Sets are excellent for mathematical operations and when you need to ensure uniqueness of elements. They're also very efficient for membership testing compared to lists.